<?php

class Zps_Controller_Action extends Zend_Controller_Action
{

    /**
     * Instance of Zend_Registry passed by Zend_Controller_Front as an
     * invocation argument.
     *
     * @var Zend_Registry
     * @access protected
     */
    protected $registry = null;

    /**
     * init() method called by Zend_Controller_Action constructor to setup
     * this Action (or its subclasses). Used here to assign FC params as
     * class properties.
     *
     * @return void
     * @access public
     */
    public function init()
    {
        $this->initRegistry();
        $this->initView();
        $this->initResponseHeaders();
    }

    protected function initRegistry()
    {
        if (!$this->getInvokeArg('Registry') instanceof Zend_Registry) {
            require_once 'Zps/Controller/Exception.php';
            throw new Zps_Controller_Exception('Registry invokation argument is not an instance of type Zend_Registry');
        }
        $this->registry = $this->getInvokeArg('Registry');
    }

    /**
     * Over-rides Zend_Controller_Action::initView()
     */
    public function initView()
    {
        $viewFactory = $this->registry->get('ViewFactory');
        $this->view = $viewFactory->createInstance( $this->getRequest()->getModuleName() );
        return $this->view;
    }

    protected function initResponseHeaders()
    {
        $charSet = $this->registry->get('Configuration')->view->encoding;
        $this->getResponse()->setHeader('Content-Type', 'text/html; charset=' . $charSet);
    }

    /**
     * Generates a 404 error for non-existing methods on all Application controllers.
     * This does not generate 404's for missing controller classes though - that needs
     * a separate piece of code attached to the MVC Dispatcher as a plugin.
     */
    public function __call($methodName, $args)
    {
        // forwards to Error404Controller::errorAction() in default Module
        $this->_forward('Error404', 'error');
    }

}